<?php

namespace App\Console\Commands;

use App\Models\Base\LogNewGuide;
use App\Service\PlayerService;
use App\Service\ServerService;

class FetchNewGuide extends CommandBase
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:guide {dateTime?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '新手引导';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dateTime = $this->argument('dateTime');
        if ($dateTime) {
            $this->today = $dateTime;
            $this->todayTimestamp = strtotime($dateTime);
            $this->yesterdayTimestamp = $this->todayTimestamp - 86400;
            $this->yesterday = date('Y-m-d', $this->yesterdayTimestamp);
        }
        $this->serverStat();
    }

    protected function serverStat()
    {
        $serverIds = ServerService::serverIds();
        if (!$serverIds) {
            return false;
        }
        foreach ($serverIds as $serverId) {
            setDB($serverId);
            $player = PlayerService::getSignPlayer($this->yesterdayTimestamp, $this->todayTimestamp);
            $playerIds = $player->pluck('id')->all();
            $player_number = count($playerIds);
            $model = new LogNewGuide();
            $model->server_id = $serverId;
            $model->date_time = $this->yesterday;
            //完成第一步新手引导
            $first_task_count = PlayerService::firstNewTask($playerIds, 1000)->count();
            $model->first_task_finish = $first_task_count;
            //未完成数量
            $undone_count = $player_number - $first_task_count;
            $model->first_task_undone = $undone_count;
            $undone_wash = $player_number ? $undone_count / $player_number : 0;
            $model->first_task_undone_wash = $undone_wash;
            $model->first_task_finish_wash = $player_number ? $first_task_count / $player_number : 0;
            //完成所有新手引导任务
            $task_count = PlayerService::firstNewTask($playerIds, 1018)->count();
            $model->task_finish_all = $task_count;
            $model->task_finish_all_wash = $player_number ? $task_count / $player_number : 0;
            $model->save();
        }
    }
}
